package cn.myblog.ainblog.conroller.error;

import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Dovaior
 * @Description
 * @create 2021-05-03 09:52
 **/
@Controller
@RequestMapping("/error")
public class ErrorController extends AbstractErrorController {

    public ErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
    //一般请求
    @RequestMapping(produces={"text/html"})
    public ModelAndView errorHtml(){
        ModelAndView mav = new ModelAndView();
        // templates/error/MyError.html
        mav.setViewName("error/MyError");
        mav.getModel().put("error", "Not Found");
        return mav;
    }
    //ajax请求
    @RequestMapping
    @ResponseBody
    public Map<Object,Object> error(){
        Map<Object,Object> map = new HashMap<>();
        map.put("error", "Not Found");
        return map;
    }
}
